package com.mcxx.modules.miniSalvation.fundpay.service.read;

import com.google.common.collect.Lists;
import com.mcxx.common.exception.BizException;
import com.mcxx.modules.base.utils.FundPayBizDealUtils;
import com.mcxx.modules.base.utils.QueryParamUtil;
import com.mcxx.modules.miniSalvation.family.param.SalvationFamilyParam;
import com.mcxx.modules.miniSalvation.fundpay.dao.read.FundPayMemberReadDao;
import com.mcxx.modules.miniSalvation.fundpay.dto.*;
import com.mcxx.modules.miniSalvation.fundpay.entity.FundPayFamilyEntity;
import com.mcxx.modules.miniSalvation.fundpay.entity.FundPayMemberEntity;
import com.mcxx.modules.miniSalvation.fundpay.param.FundPayFamilyParam;
import com.mcxx.modules.miniSalvation.fundpay.param.FundPayMemberParam;
import com.mcxx.modules.miniSalvation.fundpay.param.FundPayParam;
import com.mcxx.util.CommonUtil;
import com.mcxx.util.Constant;
import com.mcxx.util.DateUtil;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("fundPayMemberReadService")
@TaTransactional(readOnly = true)
public class FundPayMemberReadService extends BaseService {

    @Autowired
    private FundPayMemberReadDao fundPayMemberReadDao;


    public List<FundPayMemberEntity> getByFundPayId(String fundPayId){
        return fundPayMemberReadDao.getByFundPayId(fundPayId);
    }




    public List<FundPayMemberEntity> getCapitalMemberList(List<FundPayFamilyEntity> fundPayFamilyList, CapitalFundPayDTO capitalFundPayDTO){

        List<String> orderIdList = fundPayFamilyList.stream().map(FundPayFamilyEntity::getOrderId).collect(Collectors.toList());

        List<List<String>> groupOrderIdList = Lists.partition(orderIdList, 500);
        List<FundPayMemberEntity> fundPayMemberEntityList = new ArrayList<>();
        for(List<String> tempOrderIdList : groupOrderIdList){
            List<FundPayMemberEntity> tempFundPayMemberEntityList = fundPayMemberReadDao.getMemberCapital(tempOrderIdList, capitalFundPayDTO.getCostType(), capitalFundPayDTO.getPayDate());
            fundPayMemberEntityList.addAll(tempFundPayMemberEntityList);
        }

        if (Constant.CapitalFundPayCostType.NURSING.equals(capitalFundPayDTO.getCostType())) {
            List<FundPayMemberEntity> conditionPersons = new ArrayList<>();
            if (Constant.SupplyType.SUPPLY_DISPERSE.equals(capitalFundPayDTO.getSupplyType())) {

                conditionPersons = fundPayMemberReadDao.getCarePersonInfo(orderIdList, capitalFundPayDTO.getCareType(),capitalFundPayDTO.getPayDate());
                for (FundPayMemberEntity obj: fundPayMemberEntityList) {
                    for (FundPayMemberEntity careObj: conditionPersons) {
                        if (obj.getOrderId().equals(careObj.getOrderId()) && obj.getMemberId().equals(careObj.getMemberId())) {
                            obj.setCareId(careObj.getCareId());
                        }
                    }
                }

                fundPayMemberEntityList = fundPayMemberEntityList.stream().filter(item ->item.getCareId() != null).collect(Collectors.toList());
            } else {

                conditionPersons = fundPayMemberReadDao.getCareFeeSuspendPersonInfo(orderIdList);
                for (FundPayMemberEntity obj: fundPayMemberEntityList) {
                    for (FundPayMemberEntity careObj: conditionPersons) {
                        if (obj.getOrderId().equals(careObj.getOrderId()) && obj.getMemberId().equals(careObj.getMemberId())) {
                            obj.setCareId(careObj.getCareId());
                        }
                    }
                }

                fundPayMemberEntityList = fundPayMemberEntityList.stream().filter(item ->item.getCareId() == null).collect(Collectors.toList());
            }

        }

        if(fundPayMemberEntityList.size() <= 0){
            throw new BizException("没有可以添加到名册中的成员");
        }
        return fundPayMemberEntityList;
    }





    public  List<FundPayMemberEntity> getMemberForStand(List<FundPayFamilyEntity> list, StandFundPayDTO param){
        List<String> orderIdList = list.stream().map(FundPayFamilyEntity::getOrderId).collect(Collectors.toList());

        List<List<String>> groupOrderIdList = Lists.partition(orderIdList, 500);
        List<FundPayMemberEntity> fundPayMemberEntityList = new ArrayList<>();
        for(List<String> tempOrderIdList : groupOrderIdList){
            String dateStr = DateUtil.formatDate(param.getPayDate());
            List<FundPayMemberEntity> tempFundPayMemberEntityList = fundPayMemberReadDao.getMemberForStand(tempOrderIdList, dateStr, param.getCostType(), param.getSalaryType());
            fundPayMemberEntityList.addAll(tempFundPayMemberEntityList);
        }

        FundPayBizDealUtils.dealWithPauseMember(param,orderIdList,fundPayMemberEntityList,fundPayMemberReadDao);


        FundPayBizDealUtils.dealWithCarePersonInfo(param, orderIdList, fundPayMemberEntityList, fundPayMemberReadDao);

        if(fundPayMemberEntityList.size() <= 0){
            throw new BizException("没有可以添加到名册中的家庭");
        }
        return fundPayMemberEntityList;
    }








    public List<FundPayMemberEntity> getMemberByCreateAllowance(List<FundPayFamilyEntity> fundPayFamilyList){
        List<String> orderIdList = fundPayFamilyList.stream().map(familyEntity -> familyEntity.getOrderId()).collect(Collectors.toList());

        List<List<String>> groupOrderIdList = Lists.partition(orderIdList, 500);
        List<FundPayMemberEntity> fundPayMemberEntityList = new ArrayList<>();
        for(List<String> tempOrderIdList : groupOrderIdList){
            List<FundPayMemberEntity> tempFundPayMemberEntityList = fundPayMemberReadDao.getMemberByCreateAllowance(tempOrderIdList);
            fundPayMemberEntityList.addAll(tempFundPayMemberEntityList);
        }
        if(fundPayMemberEntityList.size() <= 0){
            throw new BizException("没有可以添加到名册中的家庭");
        }

        return fundPayMemberEntityList;
    }


    public Page getNoSaveAllowanceMemberList(AllowanceFundPayDTO allowanceFundPayDTO, PageParam pageParam){
        fundPayMemberReadDao.beginPager(pageParam);
        List<FundPayFamilyDTO> list = fundPayMemberReadDao.getNoSaveAllowanceMemberList(allowanceFundPayDTO);
        return fundPayMemberReadDao.endPager(list);
    }



    public List<FundPayMemberEntity> getMemberByAllowanceAddMember(List<String> orderIdList, List<String> memberIdList){
        return fundPayMemberReadDao.getMemberByAllowanceAddMember(orderIdList, memberIdList);
    }


    public Page getAllowanceFundPayMember(FundPayMemberParam fundPayMemberParam, PageParam pageParam, UserAccountVo userAccountVo){

        QueryParamUtil.setBaseParamValue(fundPayMemberParam, userAccountVo);
        fundPayMemberReadDao.beginPager(pageParam);

//        if (fundPayMemberParam.getGrantObject().equals(Constant.FundAllowanceGrantObject.MEMBER)) {




            List<FundPayMemberListDTO> list = fundPayMemberReadDao.getAllowanceFundPayFamily(fundPayMemberParam);
            return fundPayMemberReadDao.endPager(list);


    }








    public Page getNoSaveCustomMemberList(CustomFundPayDTO customFundPayDTO, PageParam pageParam, UserAccountVo userAccountVo){
        QueryParamUtil.setBaseParamValue(customFundPayDTO, userAccountVo);
        fundPayMemberReadDao.beginPager(pageParam);
        List<FundPayMemberDTO> list = new ArrayList<>();

        if (Constant.CapitalFundPayCostType.NURSING.equals(customFundPayDTO.getCostType())
                && Constant.SupplyType.SUPPLY_DISPERSE.equals(customFundPayDTO.getSupplyType())
        ) {
            list = fundPayMemberReadDao.getNoSaveCareMember(customFundPayDTO);
        } else {
            list = fundPayMemberReadDao.getNoSaveCustomMemberList(customFundPayDTO);
        }
        return fundPayMemberReadDao.endPager(list);
    }


    public List<String> getIdByFundPayFamilyId(String fundPayFamilyId){
        return  fundPayMemberReadDao.getIdByFundPayFamilyId(fundPayFamilyId);
    }


    public List<FundPayMemberEntity> getCustomMemberList(List<FundPayFamilyEntity> fundPayFamilyList){

        List<String> orderIdList = fundPayFamilyList.stream().map(familyEntity -> familyEntity.getOrderId()).collect(Collectors.toList());
        List<String> familyIdList = fundPayFamilyList.stream().map(familyEntity -> familyEntity.getFamilyId()).collect(Collectors.toList());

        List<List<String>> groupOrderIdList = Lists.partition(orderIdList, 500);
        List<FundPayMemberEntity> fundPayMemberEntityList = new ArrayList<>();
        for(List<String> tempOrderIdList : groupOrderIdList){
            List<FundPayMemberEntity> tempFundPayMemberEntityList = fundPayMemberReadDao.getMemberCustomForData(tempOrderIdList);
            fundPayMemberEntityList.addAll(tempFundPayMemberEntityList);
        }
        if(fundPayMemberEntityList.size() <= 0){
            throw new BizException("没有可以添加到名册中的家庭");
        }

        return fundPayMemberEntityList;
    }


    public Page getCustomMemberList(FundPayFamilyParam fundPayFamilyParam, PageParam pageParam, UserAccountVo userAccountVo){
        QueryParamUtil.setBaseParamValue(fundPayFamilyParam, userAccountVo);
        fundPayMemberReadDao.beginPager(pageParam);
        List<FundPayMemberDTO> list = new ArrayList<>();
        if (Constant.CapitalFundPayCostType.NURSING.equals(fundPayFamilyParam.getCostType())
                && Constant.SupplyType.SUPPLY_DISPERSE.equals(fundPayFamilyParam.getSupplyType())
        ) {
            list = fundPayMemberReadDao.getAlreadySaveCareMember(fundPayFamilyParam);
        } else {
            list = fundPayMemberReadDao.getCustomMemberForPage(fundPayFamilyParam);
        }
        return fundPayMemberReadDao.endPager(list);
    }







    public void cntMemberByCreateFuneral(FuneralFundPayDTO funeralFundPayDTO, UserAccountVo userAccountVo){
        QueryParamUtil.setBaseParamValue(funeralFundPayDTO, userAccountVo);
        int rst = fundPayMemberReadDao.cntMemberByCreateFuneral(funeralFundPayDTO);
        if(rst <= 0){

            throw new BizException("-1", "名册为空，无法生成");
        }
    }


    public List<FundPayMemberEntity> getMemberByCreateFuneral(FuneralFundPayDTO funeralFundPayDTO){
        return fundPayMemberReadDao.getMemberByCreateFuneral(funeralFundPayDTO);
    }


    public Page getNoSaveFuneralMemberList(FuneralFundPayDTO funeralFundPayDTO, PageParam pageParam, UserAccountVo userAccountVo){
        QueryParamUtil.setBaseParamValue(funeralFundPayDTO, userAccountVo);
        fundPayMemberReadDao.beginPager(pageParam);
        List<FundPayMemberDTO> list = fundPayMemberReadDao.getNoSaveFuneralMemberList(funeralFundPayDTO);
        return fundPayMemberReadDao.endPager(list);
    }


    public Page getFuneralMemberList(FundPayMemberParam fundPayMemberParam, PageParam pageParam, UserAccountVo userAccountVo){
        QueryParamUtil.setBaseParamValue(fundPayMemberParam, userAccountVo);
        fundPayMemberReadDao.beginPager(pageParam);
        List<FundPayMemberDTO> list = fundPayMemberReadDao.getFuneralMemberForPage(fundPayMemberParam);
        return fundPayMemberReadDao.endPager(list);
    }



    public List<FuneralFundPayExcelDTO> exportFuneralMemberList(FundPayMemberParam fundPayMemberParam){

        return  fundPayMemberReadDao.exportFuneralMemberList(fundPayMemberParam);

    }


    public List<FundPayMemberEntity> getMemberByFuneralAddMember(List<String> orderIdList, List<String> memberIdList){
        return fundPayMemberReadDao.getMemberByFuneralAddMember(orderIdList, memberIdList);
    }





    public Page  querySelect(FundPayParam param, PageParam pageParam){
        fundPayMemberReadDao.beginPager(pageParam);
        List<FundPayMemberListDTO> list = fundPayMemberReadDao.getFundPayMember(param);
        return fundPayMemberReadDao.endPager(list);
    }


    public FundPayMemberListDTO getFundPayMemberCount(FundPayParam param){
        return fundPayMemberReadDao.getFundPayMemberCount(param);
    }


    public Page getSupportPayMemberList (FundPayParam param, PageParam pageParam){
        fundPayMemberReadDao.beginPager(pageParam);
        List<FundPayMemberListDTO> list = fundPayMemberReadDao.getSupportPayMember(param);
        return fundPayMemberReadDao.endPager(list);
    }


    public Page getCarePayMemberList (FundPayParam param, PageParam pageParam){
        fundPayMemberReadDao.beginPager(pageParam);
        List<FundPayMemberListDTO> list = fundPayMemberReadDao.getCarePayMember(param);
        return fundPayMemberReadDao.endPager(list);
    }


    public List<FundPayMemberListDTO> getSupportPayMember (FundPayParam param){ return fundPayMemberReadDao.getSupportPayMember(param);}


    public List<FundPayMemberExcelDTO>  exportExcelFundPayMember(FundPayParam fundPayFamily){ return fundPayMemberReadDao.exportExcelFundPayMember(fundPayFamily); }

    public Map getDeathCountSummary(FundPayMemberParam param, UserAccountVo accountVo) {
        Map familyCountSummary;

        QueryParamUtil.setBaseParamValue(param,accountVo);
        familyCountSummary = fundPayMemberReadDao.getDeathCountSummary(param);
        return familyCountSummary;
    }

    public Map getrecevierCountSummary(FundPayMemberParam param, UserAccountVo accountVo) {
        Map familyCountSummary;

        QueryParamUtil.setBaseParamValue(param,accountVo);
        familyCountSummary = fundPayMemberReadDao.getrecevierCountSummary(param);
        return familyCountSummary;
    }


    public Page getReceiveMemberList(FundPayMemberParam fundPayMemberParam, PageParam pageParam, UserAccountVo userAccountVo){
        QueryParamUtil.setBaseParamValue(fundPayMemberParam, userAccountVo);
        fundPayMemberReadDao.beginPager(pageParam);
        List<FundPayMemberDTO> list = fundPayMemberReadDao.getReceiveMemberForPage(fundPayMemberParam);
        return fundPayMemberReadDao.endPager(list);
    }


    public void cntMemberByCreateFuneralOld(FuneralFundPayDTO funeralFundPayDTO, UserAccountVo userAccountVo) {
        QueryParamUtil.setBaseParamValue(funeralFundPayDTO, userAccountVo);
        int rst = fundPayMemberReadDao.cntMemberByCreateFuneralOld(funeralFundPayDTO);
        if(rst <= 0){

            throw new BizException("-1", "名册为空，无法生成");
        }
    }


    public List<FundPayMemberEntity> getMemberByCreateFuneralOld(FuneralFundPayDTO funeralFundPayDTO) {
        return fundPayMemberReadDao.getMemberByCreateFuneralOld(funeralFundPayDTO);
    }


    public Page getCarePersonList(FundPayMemberParam fundPayMemberParam, PageParam pageParam, UserAccountVo curUserAccount) {
        QueryParamUtil.setBaseParamValue(fundPayMemberParam, curUserAccount);
        fundPayMemberReadDao.beginPager(pageParam);
        List<FundPayMemberDTO> list = null;
        if (Constant.recevierType.MECHANISM.equals(fundPayMemberParam.getRecevierType())) {
            list = fundPayMemberReadDao.getCarePersonList(fundPayMemberParam);

        } else {
            list = fundPayMemberReadDao.getCarePersonList(fundPayMemberParam);
        }
        return fundPayMemberReadDao.endPager(list);
    }


    public Page getDisperseCapitalObjectList(FundPayMemberParam fundPayMemberParam, PageParam pageParam, UserAccountVo curUserAccount) {
        QueryParamUtil.setBaseParamValue(fundPayMemberParam, curUserAccount);
        fundPayMemberReadDao.beginPager(pageParam);
        List<FundPayMemberDTO> list = null;
        if (Constant.recevierType.MECHANISM.equals(fundPayMemberParam.getRecevierType())) {
            list = fundPayMemberReadDao.getDisperseCapitalObjectList(fundPayMemberParam);
        } else {
            list = fundPayMemberReadDao.getDisperseCapitalObjectList(fundPayMemberParam);
        }
        return fundPayMemberReadDao.endPager(list);
    }


    public List<FundPayMemberEntity> getAddAllowanceMemberByOrderIdsAndFamilyIds(List<String> orderIdList, List<String> familyIds){
        return fundPayMemberReadDao.getAddAllowanceMemberByOrderIdsAndFamilyIds(orderIdList, familyIds);
    }


    public List<FuneralFundPayTakePersonExcelDTO> exportFuneralTakePersonList(FundPayMemberParam param) {
        return  fundPayMemberReadDao.exportFuneralTakePersonList(param);
    }

    public Page getNoSaveAllowanceFamilyList(AllowanceFundPayDTO allowanceFundPayDTO, PageParam pageParam) {
        fundPayMemberReadDao.beginPager(pageParam);
        List<FundPayFamilyDTO> list = fundPayMemberReadDao.getNoSaveAllowanceFamilyList(allowanceFundPayDTO);
        return fundPayMemberReadDao.endPager(list);
    }

    public List<FundPayFamilyDetailExcelDTO> exportExcelFundPayFamily(FundPayParam param) {
        return fundPayMemberReadDao.exportExcelFundPayFamily(param);
    }

    public List<FundPayMemberEntity> getCareFeeSuspendPersonInfo(List<String> orderIdList) {
        return fundPayMemberReadDao.getCareFeeSuspendPersonInfo(orderIdList);
    }


    public List<FundPayMemberEntity> getMemberFromFundPay(List<String> familyIds, StandFundPayDTO standFundPayDTO) {
        return fundPayMemberReadDao.getMemberFromFundPay(familyIds, standFundPayDTO);
    }
}
